/**
 * Created by xingzhilong on 2017/2/14.
 */
var gulp = require("gulp"),
    del = require('del'),
    sass = require('gulp-sass'),
    rename = require('gulp-rename'),
    browserify = require('browserify'),
    source = require('vinyl-source-stream'),
    streamify = require('gulp-streamify'),
    browserSync = require('browser-sync'),
    runSequence = require('run-sequence'),
    notify = require('gulp-notify'),
    fileinclude = require('gulp-file-include'),
    uglify = require('gulp-uglify'),
    buffer = require('vinyl-buffer');


    paths = {
        dist: './dist',
        bower: './bower_components',
        js: './src/js',
        css: './src/scss',
        html: './src/html',
        images: './src/img',
        othersFont: './src/font',
        othersSwiper: './src/swiper',
        othersDateTool: './src/tools'
    };


gulp.task("clean", function (cd) {
    return del(paths.dist + '/**', cd);
});


gulp.task("copy:html", function () {
    return gulp.src([paths.html + "/*.html"])
        .pipe(fileinclude({
            prefix: '@@',
            basepath: '@file'
        }))
        .pipe(gulp.dest(paths.dist));
});
gulp.task("copy:img", function () {
    return gulp.src([paths.images + "/**"])
        .pipe(gulp.dest(paths.dist + "/img"));
});

gulp.task("copy:jquery", function () {
    return gulp.src([paths.bower + "/jquery/dist/jquery.js"])
        .pipe(gulp.dest(paths.dist + "/js/jquery"));
});

gulp.task("copy:bootjs", function () {
    return gulp.src([paths.bower + "/bootstrap/dist/js/bootstrap.js"])
        .pipe(gulp.dest(paths.dist + "/js/bootstrap"));
});

gulp.task("othersFont", function () {
    return gulp.src([paths.othersFont + '/**'])
        .pipe(gulp.dest(paths.dist + '/font'))
});

gulp.task("othersSwiper", function () {
    return gulp.src([paths.othersSwiper + '/**'])
        .pipe(gulp.dest(paths.dist + '/swiper'))
});
gulp.task("othersDateTool", function () {
    return gulp.src([paths.othersDateTool + '/**'])
        .pipe(gulp.dest(paths.dist + '/tools'))
});

gulp.task('copy', ['copy:html', 'copy:img', 'othersSwiper', 'othersDateTool', 'othersFont', 'copy:jquery', "copy:bootjs"]);

gulp.task('css', function () {
    return gulp.src([paths.css + '/index.scss'])
        .pipe(sass({
            onError: notify.onError(function(error) {
                return "Error: " + error.message;
            })
        }))
        .pipe(rename('app.css'))
        .pipe(gulp.dest(paths.dist + '/css'))
});

gulp.task('js', function () {

   return browserify(paths.js + '/index.js')
       .bundle()
       .pipe(source('app.js'))
       .pipe(buffer())
       .pipe(streamify(uglify()))
       .pipe(gulp.dest(paths.dist + '/js'))


});

gulp.task('watch', function () {
    gulp.watch(paths.html + '/**', ['copy:html']);
    gulp.watch(paths.images + '/**', ['copy:img']);
    gulp.watch(paths.css + '/**', ['css']);
    gulp.watch(paths.js + '/**', ['js']);
});

gulp.task('browser-sync', function () {
    browserSync.init({
        server: {
            baseDir: paths.dist
        },
        open: false,
        notify: false,
        port: 8003
    })
});


gulp.task('serve', ['browser-sync'], function() {
    gulp.watch(paths.dist + '/**', browserSync.reload);
});


gulp.task('build', function () {
    runSequence('clean', 'copy', 'css', 'js')
});

gulp.task('dev', function () {
    runSequence('build', 'serve', 'watch');
});